function sendAjaxRequest(type, url, postData, asyncStatus) {
    var index = layer.load(1);
    var responseData = {
        status: 0, // 0请求成功 1 请求失败
        msg: 'SUCCESS',
        data: ''
    }
    $.ajax({
        async: asyncStatus,
        type: type, //请求方式为get
        url: url,
        data: postData,
        dataType: "json",
        //请求成功完成后要执行的方法
        success: function(res){
            layer.close(index);
            responseData.msg = res.msg;
            if (res.status == 200) {
                responseData.data = res.data;
            } else {
                responseData.status = 1;
                // layer.msg(res.error);
            }
        }
    });

    return responseData
}

function reloadPage() {
    setTimeout(function () {
        location.reload();
    }, 2000);
}

function parentReloadPage() {
    setTimeout(function() {
        window.parent.location.reload();
    }, 2000);
}

function gotoPage(url) {
    setTimeout(function () {
        location.href = url;
    }, 2000);
}

function deleteDatabase(db_name) {
    layer.confirm('确定删除数据库：'+db_name+'？（删除后无法恢复，请谨慎操作！）', {
        title: '删除数据库确认',
        btn: ['我已确定','再想想'],
        icon: 7,
    }, function(){
        var postData = {
            db_name: db_name,
            _token: $('meta[name="csrf-token"]').attr('content'),
        }
        var res = sendAjaxRequest('POST', '/database/dropDatabase', postData, false);
        if (res.status == 0) {
            layer.msg('删除成功', {icon: 1});
            gotoPage('/index/index')
            // reloadPage();
        } else {
            $('#error_tip').html('<font color="red">'+res.msg+'</font>');
            // layer.msg(res.msg);
        }
    }, function(){
        // 取消操作
    });
}

function dropTable(table_name, db_name) {
    layer.confirm('确定删除数据表：'+table_name+'？（删除后无法恢复，请谨慎操作！）', {
        title: '删除数据表确认',
        btn: ['我已确定','再想想'],
        icon: 7,
    }, function(){
        var postData = {
            db_name: db_name,
            table_name: table_name,
            _token: $('meta[name="csrf-token"]').attr('content'),
        }
        var res = sendAjaxRequest('POST', '/database/dropTable', postData, false);
        if (res.status == 0) {
            layer.msg('删除成功', {icon: 1});
            gotoPage('/index/index?db_name='+db_name)
            // reloadPage();
        } else {
            $('#error_tip').html('<font color="red">'+res.msg+'</font>');
            // layer.msg(res.msg);
        }
    }, function(){
        // 取消操作
    });
}


function arrayToJson(o) {
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o)
                r.push(i + ":" + arrayToJson(o[i]));
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            for (var i = 0; i < o.length; i++) {
                r.push(arrayToJson(o[i]));
            }
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}

// 打开控制台弹窗
function openConsoleLayer() {
    layer.open({
        type: 2,
        title: '控制台',
        area: ['700px', '400px'],
        content: '/layer/console', //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
    });
}

// function changeDefault() {
//     var v = $('.default').val();
//     console.log(v);
// }